package com.dili.dd.gpi.service.impl;

import com.dili.dd.gpi.domain.Application;
import com.dili.dd.gpi.domain.ApplicationExample;
import com.dili.dd.gpi.mapper.ApplicationMapper;
import com.dili.dd.gpi.service.DmaAppService;
import com.dili.dd.gpi.util.spring.DataSourceLookupKey;
import com.dili.dd.gpi.util.spring.RequireDataSource;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by 思思君 on 2015/10/14.
 */
@Service
public class DmaAppServiceImpl implements DmaAppService
{
    @SuppressWarnings("SpringJavaAutowiringInspection")
    @Resource
    private ApplicationMapper applicationMapper;

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public int createApplyApp(Application application)
    {
        return applicationMapper.insert(application);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public List<Application> getAppList(String username)
    {
        ApplicationExample example = new ApplicationExample();
        example.createCriteria().andUsernameEqualTo(username);
        return applicationMapper.selectByExample(example);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public int deleteApp(String appId)
    {
        return applicationMapper.deleteByPrimaryKey(Integer.valueOf(appId));
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public Application getAppById(String appId)
    {
        return applicationMapper.selectByPrimaryKey(Integer.valueOf(appId));
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public Application getAppByName(String appName)
    {
        ApplicationExample example = new ApplicationExample();
        example.createCriteria().andAppNameEqualTo(appName);
        List<Application> applicationList = applicationMapper.selectByExample(example);
        if (applicationList != null && !applicationList.isEmpty())
        {
            return applicationList.get(0);
        }
        return null;
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public int updateApp(Application application)
    {
        return applicationMapper.updateByPrimaryKeySelective(application);
    }

    @Override
    @RequireDataSource(key = DataSourceLookupKey.DMA_DATASOURCE)
    public List<Application> getAllAppList()
    {
        return applicationMapper.selectByExample(new ApplicationExample());
    }
}
